<!-- 采购管理-采购任务列表 -->
<template>
  <pageHeader
    title="采购申请审核详情"
    :breadCrumb="breadCrumb"
    :show-back="true"
  >
    <template #footer>
      <a-button v-if="demand.info.state === 3" type="primary" @click="Submit">
        <template #default>提交</template>
      </a-button>
    </template>
    <div class="title">
      <h1>采购申请表</h1>
    </div>
    <a-space direction="vertical" fill>
      <div style="display: flex">
        <div class="form-item">
          <div class="title-item">
            <a-space style="margin-left: 40px">
              <div
                style="
                  width: 5px;
                  height: 23px;
                  background-color: rgba(61, 126, 255, 1);
                  border: none;
                  border-radius: 2px;
                "
              ></div>
              <div>基本信息</div></a-space
            >
          </div>
          <div class="demand-index">
            <a-form
              ref="form"
              :model="demand.info"
              autoLabelWidth
              :layout="(layout as Tlayout)"
            >
              <a-row :gutter="16">
                <a-col
                  v-for="item in demandForm?.info?.design?.form"
                  :key="item.id"
                  :span="item.span"
                >
                  <FormItem
                    :name="item.name"
                    :componentType="item.componentType"
                    v-model:props="item.props"
                    :required="item.required"
                    :disabled="item.disabled"
                    v-model:values="item.values"
                  />
                </a-col>
              </a-row>
            </a-form>
          </div>
        </div>
        <!-- <div class="approve-item">
        <h3 style="margin: 50px 225px"> </h3>
      </div> -->
      </div>
      <div v-if="demand.info.state === 6">
        <div class="title-item">
          <a-space style="margin-left: 40px">
            <div
              style="
                width: 5px;
                height: 23px;
                background-color: rgba(61, 126, 255, 1);
                border: none;
                border-radius: 2px;
              "
            ></div>
            <div>执行信息</div></a-space
          >
        </div>
        <div style="width: 95%; margin: 10px auto">
          <TableTurn
            :table-data="getData"
            ref="tableRef"
            :args="args"
            :is-show-pagination="false"
          >
            <a-table-column :width="80" align="center">
              <template #title> 序号 </template>
              <template #cell="{ rowIndex }">
                {{ rowIndex + 1 }}
              </template>
            </a-table-column>
            <a-table-column
              title="物料类型"
              :width="150"
              dataIndex="materialName"
              align="center"
            ></a-table-column>
            <a-table-column
              title="物料编码"
              :width="150"
              dataIndex="number"
              align="center"
            ></a-table-column>
            <a-table-column
              title="物料名称"
              :width="150"
              dataIndex="name"
              align="center"
            ></a-table-column>
            <a-table-column
              title="规格型号"
              :width="150"
              dataIndex="spec"
              align="center"
            ></a-table-column>
            <a-table-column
              title="采购单位"
              :width="150"
              dataIndex="basicName"
              align="center"
            ></a-table-column>
            <a-table-column
              title="需求数量"
              :width="150"
              dataIndex="amount"
              align="center"
            ></a-table-column>
            <a-table-column title="需求交付日期" :width="150" align="center"
              ><template #cell="{ record }">
                {{ dayjs(record.deliveryTime).format('YYYY-MM-DD') }}
              </template></a-table-column
            >
            <a-table-column title="状态" :width="150" align="center"
              ><template #cell="{ record }">
                <a-typography-text v-if="record.state === 1">
                  待派工
                </a-typography-text>
                <a-typography-text v-if="record.state === 2" type="primary">
                  执行中
                </a-typography-text>
              </template></a-table-column
            >
            <a-table-column
              title="关联任务单号"
              :width="150"
              dataIndex="returnNumber"
              align="center"
            >
              <template #cell="{ record }">
                {{ record.returnNumber ? record.returnNumber : '无' }}
              </template>
            </a-table-column>
            <a-table-column
              title="关联采购订单号"
              :width="150"
              dataIndex="orderNumber"
              align="center"
            >
              <template #cell="{ record }">
                {{ record.orderNumber ? record.orderNumber : '无' }}
              </template>
            </a-table-column>
            <a-table-column
              title="关联计划单号"
              :width="150"
              dataIndex="planNumber"
              align="center"
            >
              <template #cell="{ record }">
                {{ record.planNumber ? record.planNumber : '无' }}
              </template>
            </a-table-column>
          </TableTurn>
        </div>
      </div>

      <div>
        <div class="title-item">
          <a-space style="margin-left: 40px">
            <div
              style="
                width: 5px;
                height: 23px;
                background-color: rgba(61, 126, 255, 1);
                border: none;
                border-radius: 2px;
              "
            ></div
            ><div>审核结果</div></a-space
          >
        </div>

        <div style="width: 95%; margin: 20px 40px">
          <a-space :size="40" direction="vertical" fill>
            <div
              ><span>是否通过审核：</span>
              <a-radio-group
                v-model="Process"
                size="large"
                :disabled="demand.info.state !== 3"
              >
                <a-radio
                  :value="1"
                  :style="{
                    marginRight: '20px',
                  }"
                >
                  <template #radio="{ checked }">
                    <div
                      :style="{
                        display: 'flex',
                        width: '135px',
                      }"
                      :class="checked ? 'checked-radio' : 'unchecked-radio'"
                    >
                      <div
                        :style="{
                          width: '35px',
                          textAlign: 'center',
                        }"
                        :class="
                          checked
                            ? 'radio-icon-box-checked'
                            : 'radio-icon-box-unchecked'
                        "
                      >
                        <icon-check-circle
                          :class="
                            checked
                              ? 'radio-icon-checked'
                              : 'radio-icon-unchecked'
                          "
                          class="radio-icon"
                          size="20px"
                        />
                      </div>
                      <div
                        :style="{
                          width: '100px',
                          textAlign: 'center',
                        }"
                      >
                        通过
                      </div>
                    </div>
                  </template>
                </a-radio>
                <a-radio :value="2">
                  <template #radio="{ checked }">
                    <div
                      :style="{
                        display: 'flex',
                        width: '135px',
                      }"
                      :class="checked ? 'checked-radio-no' : 'unchecked-radio'"
                    >
                      <div
                        :style="{
                          width: '35px',
                          textAlign: 'center',
                        }"
                        :class="
                          checked
                            ? 'radio-icon-box-checked-no'
                            : 'radio-icon-box-unchecked'
                        "
                      >
                        <icon-check-circle
                          :class="
                            checked
                              ? 'radio-icon-checked-no'
                              : 'radio-icon-unchecked'
                          "
                          class="radio-icon"
                          size="20px"
                        />
                      </div>
                      <div
                        :style="{
                          width: '100px',
                          textAlign: 'center',
                        }"
                      >
                        不通过
                      </div>
                    </div>
                  </template>
                </a-radio>
              </a-radio-group></div
            >
          </a-space>
        </div>
      </div>
    </a-space>
  </pageHeader>
</template>

<script lang="ts" setup>
  import {
    reactive,
    ref,
    computed,
    onMounted,
    defineAsyncComponent,
  } from 'vue';
  import { useRouter, useRoute } from 'vue-router';
  import { Message } from '@arco-design/web-vue';
  import dayjs from 'dayjs';
  import {
    postPurchaseApply,
    getSinglePurchaseApply,
    PutPurchaseApplyList,
  } from '@/api/buy-management/buy-apply/index';
  import { getOpenTemp } from '@/api/buy-management/get-template/index';

  const getrefs = ref();
  const route = useRoute();
  const router = useRouter();
  const layout = ref('vertical');
  type Tlayout = 'inline' | 'horizontal' | 'vertical' | undefined;
  const breadCrumb: string[] = ['采购管理', '采购管理', '采购申请审核详情'];

  const demand = reactive<any>({
    info: {
      number: '',
      name: '',
      applyByUser: 0,
      applyByDept: 0,
      phone: '',
      applyTime: '',
      deliveryTime: '',
      priority: 0,
      state: 0,
      cause: '',
      templateId: 0,
      flowId: 0,
      file_id: 0,
      extend: '',
      product: [],
      file: [],
    },
  });
  interface DemandFormType {
    info: {
      id: number;
      flowId: number;
      design: {
        form: any[];
      };
    };
  }
  const demandForm = reactive<DemandFormType>({
    info: { id: 0, flowId: 0, design: { form: [] } },
  });

  const orderRespData = ref([]);

  const Process = ref(1);
  const getTemp = async () => {
    const res = await getSinglePurchaseApply({ id: Number(route.query.id) });
    orderRespData.value = res.orderResp;
    demand.info = res.applyData;
    demandForm.info.design.form = res.applyData.extends;
    demandForm.info.design.form.sort((a: any, b: any) => a.sort - b.sort);
    demandForm.info.design.form.forEach((item: any) => {
      item.disabled = true;
      if (item.values !== '') {
        if (item.values[0] === '') {
          item.values = [];
        }
      }
      item.values[0] = demand.info[item.fieldName];
      if (item.code === 'FILE') {
        item.props.data = demand.info.file;
      }
      if (item.code === 'APPLY_DETAILED') {
        item.props.data = demand.info.product;
        item.props.state = 1;
      }
      if (item.code === 'APPLY_DATE') {
        item.values = [demand.info.createdAt];
      }
    });
    if (demand.info.state === 6) {
      Process.value = 1;
      const NewMsg = demandForm.info.design.form.findIndex(
        (i) => i.code === 'APPLY_DETAILED'
      );
      demandForm.info.design.form.splice(NewMsg, 1);
    } else if (demand.info.state === 5) {
      Process.value = 2;
    }
  };
  getTemp();
  const visible = ref(false);
  const keys = Object.keys(demand.info);
  const handleClick = () => {
    visible.value = true;
  };

  const handleOk = async () => {
    visible.value = false;
    demandForm.info.design.form.forEach((item) => {
      if (item.fieldName && keys.includes(item.fieldName)) {
        demand.info[item.fieldName] = item.values[0];
      }
    });
    demand.info.extend = JSON.stringify(demandForm.info.design.form);
    demand.info.applyTime = new Date().toJSON();
    demand.info.deliveryTime = new Date().toJSON();
    demand.info.state = 2;
    await postPurchaseApply(demand.info);
  };
  const handleCancel = () => {
    visible.value = false;
  };

  // 保存
  const save = async () => {
    demandForm.info.design.form.forEach((item) => {
      if (item.fieldName && keys.includes(item.fieldName)) {
        demand.info[item.fieldName] = item.values[0];
      }
      if (item.code === 'APPLY_DETAILED') {
        demand.info.product = item.props.data;
      }
    });
    demand.info.product.forEach((item) => {
      item.deliveryTime = new Date(item.deliveryTime).toJSON();
    });
    demand.info.extend = JSON.stringify(demandForm.info.design.form);
    demand.info.applyTime = new Date().toJSON();
    demand.info.deliveryTime = new Date().toJSON();
    await PutPurchaseApplyList(demand.info);
  };

  const getData = async () => {
    try {
      const res = await getSinglePurchaseApply({ id: Number(route.query.id) });
      return Promise.resolve(res.applyData.product);
    } catch (err: unknown) {
      console.log(err);
    }
  };

  const Submit = async () => {
    demandForm.info.design.form.forEach((item) => {
      if (item.fieldName && keys.includes(item.fieldName)) {
        demand.info[item.fieldName] = item.values[0];
      }
      if (item.code === 'APPLY_DETAILED') {
        demand.info.product = item.props.data;
      }
    });
    demand.info.product.forEach((item) => {
      item.deliveryTime = new Date(item.deliveryTime).toJSON();
    });
    demand.info.extend = JSON.stringify(demandForm.info.design.form);
    demand.info.applyTime = new Date().toJSON();
    demand.info.deliveryTime = new Date().toJSON();
    if (!Process.value) {
      Message.error('请选择审核结果');
      return;
    }
    if (Process.value === 1) {
      demand.info.state = 6;
    } else {
      demand.info.state = 5;
    }
    await PutPurchaseApplyList(demand.info);
    router.back();
  };

  onMounted(() => {});
</script>

<style scoped lang="less">
  // .content-head {
  // }
  .demand-index {
    padding: 25px 140px;
  }
  .title {
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .title-item {
    width: 95%;
    height: 60px;
    margin: auto;
    background-color: rgba(245, 247, 250);
    line-height: 60px;
    margin-top: 20px;
  }

  .unchecked-radio {
    background-color: #fff;
    color: #dcdfe6;
    border: 1px solid #dcdfe6;
    font-weight: 700;
    font-size: 16px;
  }
  .checked-radio {
    background-color: rgb(49, 194, 124);
    color: #fff;
    border: 1px solid rgb(49, 194, 124);
    font-weight: 700;
    font-size: 16px;
  }

  .checked-radio-no {
    background-color: rgb(255, 0, 0);
    color: #fff;
    border: 1px solid rgb(255, 0, 0);
    font-weight: 700;
    font-size: 16px;
  }
  .radio-icon-box-checked {
    border: 1px solid rgb(49, 194, 124);
    background-color: #fff;
  }
  .radio-icon-box-checked-no {
    border: 1px solid rgb(255, 0, 0);
    background-color: #fff;
  }
  .radio-icon-box-unchecked {
    border: 1px solid rgba(220, 223, 230, 1);
  }
  .radio-icon-checked {
    color: rgb(49, 194, 124);
  }

  .radio-icon-checked-no {
    color: rgb(255, 0, 0);
  }

  .radio-icon-unchecked {
  }
  .radio-icon {
    position: relative;
    top: 3px;
  }

  .approve-item {
    margin: 25px auto;
    width: 35%;
    background-color: rgba(255, 255, 255, 1);
    border-radius: 8px;
    box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.349019607843137);
    height: 600px;
  }
  .form-item {
    width: 100%;
  }

  // :deep(.arco-input[disabled]) {
  //   -webkit-text-fill-color: black;
  // }
  // :deep(.arco-textarea[disabled]) {
  //   -webkit-text-fill-color: black;
  // }
</style>
